#include "stdafx.h"
#include "Keyboard.h"

#include "Log.h"
#include "Exception.h"
#include "ErrorHandlers.h"
#include "MemUtils.h"

namespace Cvekas {

Keyboard::Keyboard()
{
	ZeroMemory(&keyboard[0], sizeof(keyboard));
	ZeroMemory(&keyboard_current[0], sizeof(keyboard_current));
	ZeroMemory(&keyboard_last[0], sizeof(keyboard_last));

	LOG("Keyboard initialized");
}

Keyboard::~Keyboard()
{
	LOG("Keyboard destroyed");
}

void Keyboard::update()
{
	memcpy(keyboard_last, keyboard, sizeof(keyboard));
	memcpy(keyboard, keyboard_current, sizeof(keyboard));
}

bool Keyboard::isKeyPressed(Key key) const
{
	return keyboard[key];
}

bool Keyboard::isKeyUp(Key key) const
{
	return (keyboard_last[key] && !keyboard[key]);
}

bool Keyboard::isKeyDown(Key key) const
{
	return (!keyboard_last[key] && keyboard[key]);
}

bool Keyboard::process(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_KEYDOWN:
		keyboard_current[wParam & 0xFF] = true;
		break;
	case WM_KEYUP:
		keyboard_current[wParam & 0xFF] = false;
		break;

	default:
		return false;
	}
	return true;
}

} // namespace